<?php
// +------------------------------------------------------------------
// | Cls-Token [ USER LOGIN AUTHENTICATION TOKEN ]
// | Copyright (c) 2024 All rights reserved.
// | Based on ThinkPHP 6 | 8
// | Licensed Mulan PSL v2 ( http://license.coscl.org.cn/MulanPSL2 )
// | Author: CLS <422064377>
// | CreateDate: 2024-07-16
// +------------------------------------------------------------------

namespace chleniang\ClsToken\exception;


use Exception;

class ClsTokenException extends Exception
{
    protected $message = 'CLS-Token exception.';

    protected $code = 0;


    /**
     * 异常HTTP状态码
     */
    protected int $status_code = 401;

    /**
     * 异常附带返回响应头
     */
    protected array $headers = [];

    /**
     * 异常附带返回数据
     */
    protected array $data = [];


    /**
     * ClsToken异常
     *
     * @param array $params  异常参数
     *                       [
     *                       "msg" => (string)异常message文字信息,
     *                       "code" => (int)异常代码(默认 0),
     *                       "status_code" => (int)HTTP状态码(默认 401),
     *                       "data" => (array)异常附带返回数据,
     *                       "headers" => (array)HTTP响应头附加信息,
     *                       ]
     */
    public function __construct(array $params = [])
    {
        if (!empty($params['msg'])) {
            $this->message = $params['msg'];
        }
        if (!empty($params['code'])) {
            $this->code = $params['code'];
        }
        if (!empty($params['status_code'])) {
            $this->status_code = $params['status_code'];
        }
        if (!empty($params['data'])) {
            $this->data = $params['data'];
        }
        if (!empty($params['headers'])) {
            $this->headers = $params['headers'];
        }

        parent::__construct($this->message, $this->code);
    }


    /**
     * 获取异常HTTP状态码
     *      方法名与 TP -> HTTPException中的保持一致
     */
    public function getStatusCode(): int
    {
        return $this->status_code;
    }

    /**
     * 获取异常HTTP响应头
     *      方法名与 TP -> HTTPException中的保持一致
     */
    public function getHeaders(): array
    {
        return $this->headers;
    }

    /**
     * 获取异常响应附带数据
     */
    public function getData(): array
    {
        return $this->data;
    }

}